#!/usr/bin/python
# -*- coding: utf-8 -*-
#-------------------------------------------------
#Create At: 2009-4-21 下午03:13:29 
#Create By: Fla.sam
#
#DESC: 
#
#-------------------------------------------------
from annoying.decorators import render_to
from core_app.forms import StudentRegForm, StudentStatusForm
from core_app.models import ProFtpdUser
from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404
import os
import os.path

# Create your views here.   

@render_to( 'core_app/index.html' )
def pure_index_view( request ):
    
    try:
        readme_fp = open( settings.FTPD_README, 'r' )
    except:
        readme = 'README'
    else:
        readme = readme_fp.read()
        readme_fp.close()
        
    try:
        news_fp = open( settings.FTPD_NEWS, 'r' )
    except:
        news = 'No news is good news.'
    else:
        news = news_fp.read()
        news_fp.close()
            
    if request.method == 'POST':
        form = StudentRegForm( request.POST )
        if form.is_valid():
            u = form.save( commit = False )
            #now set other fields
            u.passwd = form.cleaned_data['password2']
            u.save()
            return HttpResponseRedirect( reverse( 'ftp_user_info_view', kwargs = {'id':u.id} ) )
        d = {'form':form,
             'readme':readme,
             'news':news}
        return d
    form = StudentRegForm()
    d = {'form':form,
         'readme':readme,
         'news':news}
    return d

@render_to( 'core_app/status.html' )
def ftp_user_status_view( request ):
    
    if request.method == 'POST':
        form = StudentStatusForm( request.POST )
        if form.is_valid():
            user_obj = form.save()
            return HttpResponseRedirect( reverse( 'ftp_user_info_view', kwargs = {'id':user_obj.id} ) )
        d = {'form':form}
        return d
    form = StudentStatusForm()
    d = {'form':form}
    return d
    
@render_to( 'core_app/info.html' )
def ftp_user_info_view( request, id ):
    
    user_obj = get_object_or_404( ProFtpdUser, id = id )
    ftp_info = settings.FTPD_HOST_PORT
    d = {'ftp_user_obj':user_obj,
         'ftp_info':ftp_info}
    return d
    


